<template>
  <div class="_wz_cent_main _wz_Incumbency">
    <Card dis-hover :bordered="true" style="margin:0 0 10px;">
      <p slot="title">搜索</p>
      <div slot="extra">
        <Button type="info" size="small" v-if="!searchType" @click="searchType = true" icon="ios-arrow-down">更多</Button>
        <Button type="info" size="small" v-else @click="searchType = false" icon="ios-arrow-up">收起</Button>
      </div>
      <Form :model="formDataSearch" label-position="right" :label-width="100" inline>
        <FormItem label="从业人员">
          <i-input v-model="formDataSearch.name" placeholder="从业人员" clearable />
        </FormItem>
        <FormItem label="联系电话">
          <i-input v-model="formDataSearch.phone" placeholder="联系电话" clearable />
        </FormItem>
        <FormItem label="身份证号" v-if="searchType && (!seeCarData || !seeCarData.carPlateNo)">
          <i-input v-model="formDataSearch.identifyNum" placeholder="身份证号" clearable />
        </FormItem>
        <FormItem label="驾乘车辆" v-if="searchType && (!seeCarData || !seeCarData.carPlateNo)">
          <i-input v-model="formDataSearch.vehiclePlate" placeholder="驾乘车辆" clearable />
        </FormItem>
        <FormItem label="从业类型" v-if="searchType">
          <i-select v-model="formDataSearch.type" filterable placeholder="全部类型" clearable>
            <Option v-for="item in personType" :value="item.code" :key="item.code">{{item.value}}</Option>
          </i-select>
        </FormItem>
        <FormItem label="授权状态" v-if="searchType">
          <i-select v-model="formDataSearch.authState" filterable placeholder="请选择授权状态" clearable>
            <Option value="1">已授权</Option>
            <Option value="0">未授权</Option>
          </i-select>
        </FormItem>
        <FormItem label="审核状态" v-if="searchType">
          <i-select v-model="formDataSearch.binding" filterable placeholder="请选择审核状态" clearable>
            <Option value="0">待审核</Option>
            <Option value="1">在职</Option>
          </i-select>
        </FormItem>
        <FormItem label="人脸认证" v-if="searchType">
          <i-select v-model="formDataSearch.perfectState" filterable placeholder="请选择人脸认证" clearable>
            <Option value="0">未认证</Option>
            <Option value="1">已认证</Option>
          </i-select>
        </FormItem>
        <FormItem label="身份认证" v-if="searchType">
          <i-select v-model="formDataSearch.personState" filterable placeholder="请选择身份认证" clearable>
            <Option value="0">未认证</Option>
            <Option value="1">已认证</Option>
          </i-select>
        </FormItem>
        <FormItem label="驾驶证认证" v-if="searchType">
          <i-select v-model="formDataSearch.licenseState" filterable placeholder="请选择驾驶证认证" clearable>
            <Option value="0">未认证</Option>
            <Option value="1">已认证</Option>
          </i-select>
        </FormItem>
        <FormItem label="从业资格证" v-if="searchType">
          <i-select v-model="formDataSearch.certificateState" filterable placeholder="请选择从业资格证" clearable>
            <Option value="0">未认证</Option>
            <Option value="1">已认证</Option>
          </i-select>
        </FormItem>
        <FormItem label="合同认证" v-if="searchType">
          <i-select v-model="formDataSearch.contractState" filterable placeholder="请选择合同认证" clearable>
            <Option value="0">未认证</Option>
            <Option value="1">已认证</Option>
          </i-select>
        </FormItem>
        <FormItem label="归属部门" v-if="searchType">
          <i-input v-model="formDataSearch.inOrgName" placeholder="请填写归属部门" clearable />
        </FormItem>
        <FormItem label="运输企业" v-if="searchType && !seeCarData">
          <i-select v-model="formDataSearch.deptUuid" filterable placeholder="请选择运输企业" clearable>
            <Option v-for="item in subDeptList" :value="item.uuid" :key="item.uuid">{{ item.deptName }}</Option>
          </i-select>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="searchSubmit">搜索</Button>
        </FormItem>
      </Form>
    </Card>
    <Card dis-hover :bordered="true">
      <p slot="title">
        列表
        <span style="color: #ed4014;margin: 0 0 0 10px;font-weight: 600;font-size: 12px;">如无法授权或授权次数不足，请联系商务人员。</span>
      </p>
      <p slot="extra" v-if="!seeCarData">
        <Button type="success" @click="expireDerive">导出</Button>
      </p>
      <wzTbale ref="wzTbale" :tableData="tableData"></wzTbale>
    </Card>

    <Modal v-model="processingOperations" title="处理操作" :mask-closable="false">
      <i-input v-model="reason" type="textarea" :rows="4" placeholder="请说明处理意见" />
      <div slot="footer">
        <Button type="text" size="large" @click="processingOperations = false">取消</Button>
        <Button type="primary" size="large" @click="reasonFun">确定</Button>
      </div>
    </Modal>

    <Drawer title="详情" v-model="seeMoreType" width="900" :mask-closable="false" :draggable="true" :styles="formOperationStyles">
      <div v-if="seeMoreType">
        <Spin size="large" fix v-if="spinShow1"></Spin>
        <userData :selectData="selectData"></userData>
        <div class="demo-drawer-footer">
          <template v-if="userType != 0">
            <!--        入职 离职 show -->
            <Button type="success" @click="reasonTypeFun(1)" v-if="userType == 2">审核通过</Button>
            <Button type="warning" @click="reasonTypeFun(2)" v-if="userType == 2">审核驳回</Button>
            <!--        入职 show-->
            <Button type="primary" @click="studentAuthorization" v-if="userType == 3">学习授权</Button>
            <Button type="error" @click="reasonTypeFun(1,2)" v-if="userType == 4">离职解聘</Button>
          </template>
        </div>
      </div>

    </Drawer>



    <!--添加修改-->
    <Drawer :title="formTitle" v-model="formOperation" width="610" :mask-closable="false" :draggable="true" :styles="formOperationStyles">
      <IncumbencyAdd v-if="formOperation" @closeFun="closeFun" :selectData="selectData"></IncumbencyAdd>
    </Drawer>

    <Spin size="large" fix v-if="spinShow"></Spin>

  </div>
</template>
<script>
  import userData from './userData.vue'
  import IncumbencyAdd from './IncumbencyAdd.vue'
  import wzTbale from "../../../components/table/table";

  export default {
    props: ['seeCarData'],//接收来自父组件的数据
    components: {
      userData,//详情
      IncumbencyAdd,//详情
      wzTbale,//列表
    },
    data() {
      return {


        spinShow1:false,
        spinShow:false,//本页加载
        searchType:false,//搜索条件
        formTitle:'',
        formOperation:false,
        formOperationStyles: {//表单左侧移出css
          height: 'calc(100% - 55px)',
          overflow: 'auto',
          paddingBottom: '53px',
          position: 'static'
        },
        subDeptList: [],//运输企业
        formDataSearch: {
          deptUuid:!!this.seeCarData?this.seeCarData.deptUuid:'',
          vehiclePlate:!!this.seeCarData?this.seeCarData.carPlateNo:'',

        },//搜索表单数据
        selectData: {},//点选数据
        personType: [],//从业类型
        tableData: {
          url: this.apiUrl.coreDriverCoreDriverPageList,//请求地址
          method: 'post',//请求方式
          where: {deptUuid:!!this.seeCarData?this.seeCarData.deptUuid:'',vehiclePlate:!!this.seeCarData?this.seeCarData.carPlateNo:'',},//搜索条件
          height: 'auto',//表格高度 auto
          page: true,//分页是否使用
          columns: [//表头数据
            {type: 'index', width: 70, align: 'center', title: '序号'},
            {title: '从业人员', minWidth: 90, key: 'name'},
            {title: '人员状态', minWidth: 140, key: 'customNo', render: (h, params) => {
                if(params.row.binding == 0){
                  if (params.row.jobState == 1) {
                    return h('Tag',{  props: {type: 'dot', color: 'success'}}, '入职申请');
                  }else{
                    return h('Tag',{  props: {type: 'dot', color: 'warning'}}, '离职申请');
                  }
                }else{
                  if (params.row.authState == 1) {
                    return h('Tag',{  props: { type: 'dot',color: 'primary'}}, '已授权');
                  }else{
                    return h('Tag',{  props: {type: 'dot',color: 'warning'}}, '未授权');
                  }
                }
              }
            },
            {title: '从业类型', minWidth: 100, key: 'typeName'},
            {title: '联系电话', minWidth: 120, key: 'phone'},
            {title: '驾乘车辆', minWidth: 110, key: 'vehiclePlate'},
            {title: '身份证', minWidth: 160, key: 'identifyNum'},
            {title: '入职时间', minWidth: 160, key: 'entryTime'},
            {
              title: '到期时间', minWidth: 160, render: (h, params) => {
                return h('span',  !!params.row.authTime ? params.row.authTime : "--");
              }
            },
            {title: '归属部门', minWidth: 160, key: 'inOrgName'},
            {title: '运输企业', minWidth: 260, key: 'deptName'},
            {title: '人脸认证', minWidth: 140, key: 'perfectState', render: (h, params) => {
                if (params.row.perfectState == 1) {
                  return h('Tag',{  props: {type: 'dot', color: 'success'}}, '已认证');
                }else{
                  return h('Tag',{  props: {type: 'dot', color: 'warning'}}, '未认证');
                }
              }
            },
            {title: '身份认证', minWidth: 140, key: 'personState', render: (h, params) => {
                if (params.row.personState == 1) {
                  return h('Tag',{  props: {type: 'dot', color: 'success'}}, '已认证');
                }else{
                  return h('Tag',{  props: {type: 'dot', color: 'warning'}}, '未认证');
                }
              }
            },
            {title: '驾驶证认证', minWidth: 140, key: 'licenseState', render: (h, params) => {
                if (params.row.licenseState == 1) {
                  return h('Tag',{  props: {type: 'dot', color: 'success'}}, '已认证');
                }else{
                  return h('Tag',{  props: {type: 'dot', color: 'warning'}}, '未认证');
                }
              }
            },
            {title: '从业资格', minWidth: 140, key: 'certificateState', render: (h, params) => {
                if (params.row.certificateState == 1) {
                  return h('Tag',{  props: {type: 'dot', color: 'success'}}, '已认证');
                }else{
                  return h('Tag',{  props: {type: 'dot', color: 'warning'}}, '未认证');
                }
              }
            },
            {title: '合同认证', minWidth: 140, key: 'contractState', render: (h, params) => {
                if (params.row.contractState == 1) {
                  return h('Tag',{  props: {type: 'dot', color: 'success'}}, '已认证');
                }else{
                  return h('Tag',{  props: {type: 'dot', color: 'warning'}}, '未认证');
                }
              }
            },
            {
              title: '操作', key: 'value', fixed: 'right', width: 120, render: (h, params) => {
                let operationButtons = [];
                //userType 1 查看   2 审核    3 授权    4 解聘
                //查看
                operationButtons.push(
                  h('span', {
                    style: {"color": "#2d8cf0", "cursor": "pointer"},
                    on: {
                      click: () => {
                        this.selectData = JSON.parse(JSON.stringify(params.row));
                        this.userType = 1;
                        this.seeMoreType = true;
                      }
                    }
                  }, '查看')
                );
                var buttonList = [];

                //修改
                buttonList.push(h('DropdownItem', {
                  nativeOn: {
                    click: () => {
                      this.formTitle = "修改";
                      this.selectData = JSON.parse(JSON.stringify(params.row))
                      this.formOperation = true;
                    }
                  }
                }, '修改'))

                if(params.row.binding == 0){
                  //审核
                  buttonList.push(h('DropdownItem', {
                    nativeOn: {
                      click: () => {
                        this.selectData = JSON.parse(JSON.stringify(params.row));
                        this.userType = 2;
                        this.seeMoreType = true;
                      }
                    }
                  }, '审核'))
                }
                if(params.row.authState == 0){
                  //授权
                  buttonList.push(h('DropdownItem', {
                    nativeOn: {
                      click: () => {
                        this.selectData = JSON.parse(JSON.stringify(params.row));
                        this.userType = 3;
                        this.seeMoreType = true;
                      }
                    }
                  }, '授权'))
                }

                if(params.row.binding != 0){
                  //解聘
                  buttonList.push(h('DropdownItem', {
                    nativeOn: {
                      click: () => {
                        this.selectData = JSON.parse(JSON.stringify(params.row));
                        this.userType = 4;
                        this.seeMoreType = true;
                      }
                    }
                  }, '解聘'))
                }

                if (buttonList.length > 0 && !this.seeCarData) {
                  if(operationButtons.length > 0){
                    operationButtons.push(h('Divider', {props: {type: 'vertical',}}, ''))
                  }
                  operationButtons.push(h('Dropdown', {props: {trigger: "hover", transfer: true}},
                    [
                      h('span', {style: {"color": "#2d8cf0", "cursor": "pointer"}}, '更多▼'),
                      h('DropdownMenu', {slot: "list"}, buttonList)
                    ])
                  )
                }
                return operationButtons;
              }
            },
          ],
          format: {//返回数据格式
            list: ['data'],//数据list
            count: ['count'],//数据总量
          },
        },


        seeMoreType: false,//查看
        userType:1,// 1 查看   2 审核    3 授权    4 解聘

        processingOperations: false,//处理操作
        reason: '同意',//离职理由理由
        reasonType: "",//审核类型
        jobState: 0,//解聘使用
      }
    },
    beforeCreate() {//beforeCreate创建前状态

    },
    created() {//created创建完毕状态
    },
    beforeMount() {//beforeMount挂载前状态   DOM 元素然后塞入页面的过程

    },
    mounted() {//mounted 挂载结束状态
      let that = this;

      //运输企业
      that.axios.post(that.apiUrl.coreDeptCoreDeptList,{}).then(res => {
        if (!!res) {
          that.subDeptList = res.data.data;
        }
      }).catch(err => {
        console.log("失败", err)
      })


      //从业类型
      that.axios.post(that.apiUrl.systemDictSystemDictList, {type: 'person_type'}).then(res => {
        if (!!res) {
          that.personType = res.data.data;
        }
      }).catch(err => {
        console.log('失败', err);
      })
    },
    methods: {//执行的方法
      //搜索
      searchSubmit() {
        this.tableData.where = JSON.parse(JSON.stringify(this.formDataSearch));
        this.$refs.wzTbale.tableLoadData(true);
      },
      studentAuthorization() {
        let that = this;
        if (!!that.selectData) {
          that.$Modal.confirm({
            title: '授权提醒',
            content: '确定要授权选中的学员？',
            onOk: () => {
              that.spinShow1 = true;
              that.axios.post(that.apiUrl.coreDriverCoreAuthRecordAuthOperate, {driverUuid: that.selectData.uuid}).then(res => {
                that.spinShow1 = false;
                if (!!res) {
                  that.$Message.success('授权成功');
                  that.$refs.wzTbale.tableLoadData();
                  that.seeMoreType = false;
                }
              }).catch(err => {
                console.log('失败', err);
              })
            },
            onCancel: () => {
              //点击取消
            }
          });
        } else {
          that.$Message.error('请选择一条数据');
        }
      },
      //审核请求
      reasonTypeFun(data, type) {
        let that = this;
        if (!!that.selectData) {
          that.$Modal.confirm({
            title: '审核说明',
            content: '确定要审核选中的学员？',
            onOk: () => {
              that.reason = "同意";
              that.reasonType = data;
              that.jobState = !!type ? type : that.selectData.jobState;
              // that.processingOperations = true;
              that.reasonFun();
            },
            onCancel: () => {
              //点击取消
            }
          });
        } else {
          that.$Message.error('请选择一条数据');
        }
      },
      //审核提交
      reasonFun() {
        let that = this;
        if (that.reason.length > 0) {
          let objData = {
            auditState: that.reasonType,
            driverUuid: that.selectData.uuid,
            remark: that.reason,
            operateState: that.jobState
          };
          that.$Spin.show();//显示加载
          that.axios.post(that.apiUrl.coreDriverCoreDriverOperate, objData).then(res => {
            that.$Spin.hide();//显示加载
            that.tableLoading = false;//加载
            if (!!res) {
              that.processingOperations = false;
              that.seeMoreType = false;
              that.$Message.success('审核成功');
              that.$refs.wzTbale.tableLoadData();
            }
          }).catch(err => {
            console.log('失败', err);
          })
        } else {
          that.$Message.warning("请填写审核说明");
        }
      },
      //导出
      expireDerive() {
        let that = this;
        let formDataSearch = JSON.parse(JSON.stringify(this.formDataSearch));
        that.spinShow = true;
        that.axios.post(that.apiUrl.coreDriverCoreDriverExport, formDataSearch).then(res => {
          that.spinShow = false;
          if (!!res) {
            window.open(that.apiUrl.imgUrl + res.data.data)
          }
        }).catch(err => {
          console.log("失败", err)
        })
      },
      //人员添加
      addTableData() {
        let that = this;
        that.formTitle = "添加";
        that.formOperation = true;
      },
      //添加修改触发后关闭刷新
      closeFun(data) {
        let that = this;
        if (data) {//添加修改 刷新表单
          that.searchSubmit();
        }
        that.formOperation = false;
      },
    },
    watch: {//监听

    },
    beforeUpdate() {//beforeUpdate 更新前状态

    },
    updated() {//updated 更新完成状态

    },
    beforeDestroy() {//beforeDestroy 销毁前状态

    },
    destroyed() {//destroyed 销毁完成状态

    }
  }
</script>
<style lang="less">
  ._wz_Incumbency {
    height: 100%;
    display: flex;
    flex-direction: column;

  }
</style>
